﻿using System;
namespace Calculator.Xroad.Eu.Messages {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="SubtractResponse", Namespace="http://calculator.x-road.eu", Target=typeof(SubtractResponse))]
    public class SubtractResponseSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected SubtractResponse clazz;
        
        public SubtractResponseSerializer(SubtractResponse clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXmlAttributes(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXmlAttributes(Writer);
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("request");
            if (clazz.Request!=null) {
                global::Xtee.Core.SerializationMetadata metaRequest = this.GetMetaData(clazz.Request);
                if (ShouldWriteTypeAttribute(typeof(global::Calculator.Xroad.Eu.Variables), clazz.Request.GetType())) {
                    Writer.WriteTypeAttribute(metaRequest.TypeQualifiedName.Namespace, metaRequest.TypeQualifiedName.Name);
                }
                var requestSerializer = metaRequest.NewSerializer(clazz.Request);
                requestSerializer.WriteXmlAttributes(Writer);
                requestSerializer.WriteXml(Writer);
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Request");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("Result");
            if (clazz.Result!=null) {
                global::Xtee.Core.SerializationMetadata metaResult = this.GetMetaData(clazz.Result);
                if (ShouldWriteTypeAttribute(typeof(global::Calculator.Xroad.Eu.Result), clazz.Result.GetType())) {
                    Writer.WriteTypeAttribute(metaResult.TypeQualifiedName.Namespace, metaResult.TypeQualifiedName.Name);
                }
                var resultSerializer = metaResult.NewSerializer(clazz.Result);
                resultSerializer.WriteXmlAttributes(Writer);
                resultSerializer.WriteXml(Writer);
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Result");
            }
            Writer.WriteEndElement();
        }
        
        public override void ReadXmlAttributes(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXmlAttributes(Reader);
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="request") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>>>SubtractResponse>request");
                }
                global::Xtee.Core.SerializationMetadata metaRequest = this.GetMetaData<global::Calculator.Xroad.Eu.Variables>(Reader, "Variables", "http://calculator.x-road.eu");
                clazz.Request = metaRequest.NewEntity<global::Calculator.Xroad.Eu.Variables>();
                if ((Reader.IsEmptyElement == false)) {
                    var requestSerializer = metaRequest.NewSerializer(clazz.Request);
                    requestSerializer.ReadXmlAttributes(Reader);
                    Reader.ReadStartElement("request");
                    requestSerializer.ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>>>SubtractResponse>request", Reader.LocalName);
            }
            if (Reader.LocalName=="Result") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>>>SubtractResponse>Result");
                }
                global::Xtee.Core.SerializationMetadata metaResult = this.GetMetaData<global::Calculator.Xroad.Eu.Result>(Reader, "Result", "http://calculator.x-road.eu");
                clazz.Result = metaResult.NewEntity<global::Calculator.Xroad.Eu.Result>();
                if ((Reader.IsEmptyElement == false)) {
                    var resultSerializer = metaResult.NewSerializer(clazz.Result);
                    resultSerializer.ReadXmlAttributes(Reader);
                    Reader.ReadStartElement("Result");
                    resultSerializer.ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>>>SubtractResponse>Result", Reader.LocalName);
            }
        }
    }
}
